package com.aracky.common.annotation.dataconvert;

import com.aracky.common.annotation.DataDeal;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author zengzhiping
 * @ClassName DataAspect
 * @Description 数据切面
 * @createTime 2022年06月13日
 */
@Component
@Aspect
public class DataAspect {

    @Autowired
    private DataResultUtil dataResultUtil;


    @AfterReturning(pointcut = "@annotation(com.aracky.common.annotation.DataDeal)", returning = "result")
    public Object dealData(JoinPoint pjp, Object result) throws Throwable {

        MethodSignature signature = (MethodSignature) pjp.getSignature();
        // 获取切入点所在的方法
        Method method = signature.getMethod();
        DataDeal annotation = method.getAnnotation(DataDeal.class);
        if (annotation == null || (!annotation.dict() && !annotation.user())) {
            return result;
        }
        //存在注解,判断注解的值然后调用对应的方法
        return dataResultUtil.translation(result, annotation);
    }



}
